package com.znsd.zl.house.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znsd.zl.house.dao.HouseDao;
import com.znsd.zl.house.entity.HouseEntity;
import com.znsd.zl.house.entity.vo.HouseRespVO;
import com.znsd.zl.house.service.DictItemServiceFeign;
import com.znsd.zl.house.service.IHouseService;
import com.znsd.zl.house.utils.ConditionPageUtils;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HouseServiceImpl
 * @Description 描述
 * @author zhubin
 * @date 2020-08-19 22:54:37
 * @Version v1.0
 */
@SuppressWarnings("all")
@Slf4j
@Service
public class HouseServiceImpl extends ServiceImpl<HouseDao, HouseEntity> implements IHouseService {

	@Autowired
	DictItemServiceFeign dictItemServiceFeign;

	@Value("${facility}")
	String facility;

	@Override
	public PageUtils conditionQueryPage(HouseRespVO houseEntity, Integer startNum, Integer size) {
		// 默认分页数
		if (startNum == null || startNum < 0) { startNum = 1; }
		if (size == null || startNum < 0) {
			size = 10;
		}
		System.out.println(houseEntity+"  "+startNum+"  "+size);
		// 计算从那开始
		Integer pageNum = (startNum -1) * size;
		// 进行查询
		List<HouseRespVO> houses = this.baseMapper.conditionQueryPage(houseEntity, pageNum, size);
		// 调用字典服务的方法
		R r = dictItemServiceFeign.queryTextByDictType(facility);
		// 获取数据并转换
		List dictItems  = (List) r.get("data");
		// 循环查询，减少数据库的操作
		for (HouseRespVO house : houses) {
			if (house.getFacility() == null) {continue;}
			StringBuilder sb = new StringBuilder();
			String[] facilitys = house.getFacility().split(",");
			if (facilitys.length == 0) {continue;}
			// 一个一个查询匹配
			for (String facility : facilitys) {
				for (Object dictItem : dictItems) {
					LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) dictItem;
					if (facility.trim().equals(map.get("id")+"")) {
						sb.append(map.get("dictItemText")+",");
						break;
					}
				}
			}
			house.setFacility(sb.toString().length() >=1 ? sb.toString().substring(0,sb.toString().length() - 1) : house.getFacility());
		}
		Integer count = conditionQueryTotal(houseEntity);
		//封装分页数据
		return ConditionPageUtils.pageData(startNum,size,houses,count);
	}

	@Override
	public PageUtils findByUserId(Integer userId, Integer page, Integer limit) {
		// 默认分页数
		if (page == null || page < 0) { page = 1; }
		if (limit == null || limit < 0) {
			limit = 10;
		}

		List<HouseRespVO> houses = this.baseMapper.findByUserId(userId, (page-1)*limit, limit);

		Integer count = this.baseMapper.findByUserIdTotal(userId);
		//封装分页数据
		return ConditionPageUtils.pageData(page,limit,houses,count);
	}

	@Override
	public Integer conditionQueryTotal(HouseRespVO house) {
		return this.baseMapper.conditionQueryTotal(house);
	}

	@Override
	public Integer saveAudit(Integer houseId) {
		Date date = new Date();
		return getBaseMapper().saveAudit(houseId,date);
	}

	@Override
	public Map<String, Object> findHouseStatus(Integer houseId) {
		return getBaseMapper().findHouseStatus(houseId);
	}

	/**
	 * 删除和批量逻辑删除
	 * @param idList  id
	 */
	@Override
	public void deleteBatch( List<Integer> idList){
		this.baseMapper.deleteBatch(idList);
	}

	@Override
	public boolean save(HouseEntity entity) {
		boolean save = super.save(entity);
		this.saveAudit(entity.getId());
		return save;
	}
}
